<?php

require_once('db.inc');
require_once('config.inc');
require_once('log.inc');

class User
{
	var $username;
	var $id;
	var $type;
	private $password;
	var $screenName;
	var $email;
	var $website;
	var $websiteTitle;
	var $IP;
	var $created;
	var $activated;

	//accessors
	public function getPassword() { return $this->password; }
	public function setPassword($pwd) 
	{
		$config = Config::getInstance();
		$this->password = MD5($config->get('siteSalt').$pwd);
	}
	
	function load($username)
	{
		$db = DB::getInstance()->getDB();

		if(!isset($db))
		{
			Log::getInstance()->write("Cannot log in to db",C_LOG_ERROR);
			return false;
		}
			
		$query = "select * from t_user where username = '".$username."'";
		$result = mysql_query($query,$db);
		
		if (!$result || mysql_num_rows($result) == 0) 
			return false;
		
		$row = mysql_fetch_assoc($result);
		
		$this->username 	= $row["username"];
		$this->type 		= (int)$row["type"];
		$this->id 			= (int)$row["id"];
		$this->password 	= $row["password"];

		$this->screenName 	= $row["screen_name"];
		$this->email 		= $row["email"];
		$this->website 		= $row["website"];
		$this->websiteTitle = $row["website_title"];
		$this->created		= $row["created"];
		$this->activated	= $row["activated"];
		
		return true;
	}
	
	function create($user,$type,$pwd,$screen,$email,$site,$siteTitle,$ip)
	{
		try
		{
			$config = Config::getInstance();
			
			if(strlen($user) == 0)
				return "Username cannot be empty";
							
			if($this->checkExists($user))
				return "User $user already exists";

			//don't forget to add captcha & check exist functionallity to registration page.

			//create user.
			$db = DB::getInstance()->getDB();
			
			$pwd = MD5($config->get('siteSalt').$pwd);
			
			$query = mysql_query("insert into t_user
				(username, type, password, screen_name,email, website, website_title, ip, activated)
				values( '".$user."',$type,'".$pwd."','".$screen."','".$email."','".$site."','".$siteTitle."','".$ip."',1);"
				,$db);
			
			if (!$query)
				throw new Exception(mysql_error());
			
			if( mysql_affected_rows() != 1)
				throw new Exception("could not create user");
				
			return "";
		}
		catch(Exception $ex)
		{
			return "Failed to create user";
		}
	}

	function update()
	{
		$config = Config::getInstance();
		
		//create user.
		$db = DB::getInstance()->getDB();
				
		$query = mysql_query("update t_user 
			set screen_name = '".$this->screenName."', 
			password = '".$this->password."' 
			where id = ".$this->id
			,$db);
		
		if (!$query)
			throw new Exception(mysql_error());
		
		if( mysql_affected_rows() != 1)
			throw new Exception("could not create user");
		
	}
	
	function checkExists($user)
	{
		try
		{
			$db = DB::getInstance()->getDB();

			$query = "select 1 from t_user where username = '".$user."'";

			$result = mysql_query($query,$db);
		
			if (!$result || mysql_num_rows($result) == 0) 
				return false;
			
			return true;
		}
		catch(exception $ex)
		{
			throw $ex;
		}
	}
	
	function checkPassword($pwd)
	{		
		$config = Config::getInstance();
		return MD5($config->get('siteSalt').$pwd) == $this->password;
	}
}
?>